package com.six.apidemic.fragment.main;

import static android.app.Activity.RESULT_OK;
import static com.six.apidemic.httpapi.Constract.SUCC_CODE;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.hms.hmsscankit.ScanKitActivity;
import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions;
import com.lzy.okgo.callback.StringCallback;
import com.six.apidemic.R;
import com.six.apidemic.adapter.ManTrapAdapter;
import com.six.apidemic.bean.LoginCallbackBean;
import com.six.apidemic.bean.ManTrapCallbackBean;
import com.six.apidemic.bean.ScanCallbackBean;
import com.six.apidemic.fragment.BaseFragment;
import com.six.apidemic.httpapi.HTTPAPI;
import com.six.apidemic.listener.OnItemClickListener;
import com.six.apidemic.util.BitmapUtil;
import com.six.apidemic.util.DateUtil;
import com.six.apidemic.util.GsonUtils;
import com.six.apidemic.util.LogUtils;
import com.six.apidemic.util.SpUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @author Administrator
 */
public class MainFragment extends BaseFragment implements View.OnClickListener {

    private TextView tvModuleToScan,tvModuleManTrap,tvModuleHint;
    private ImageView ivModule;
    public static final int CAMERA_REQ_CODE = 111;
    public static final int DECODE = 1;
    private static final int REQUEST_CODE_SCAN_ONE = 0X01;
    public LoginCallbackBean.ResultBean.UserBean bean;
    //列表相关代码
    private List<ManTrapCallbackBean.ResultBean> listManTraps = new ArrayList<>();
    private ManTrapAdapter manTrapAdapter;
    private RecyclerView rlModule;
    private String name;

    @Override
    protected void initView(View view) {
        tvModuleToScan = view.findViewById(R.id.tvModuleToScan);
        tvModuleToScan.setOnClickListener(this);
        tvModuleManTrap = view.findViewById(R.id.tvModuleManTrap);
        ivModule = view.findViewById(R.id.ivModule);
        rlModule = view.findViewById(R.id.rlModule);
        tvModuleHint = view.findViewById(R.id.tvModuleHint);
        rlModule.setLayoutManager(new GridLayoutManager(getActivity(),2));
        manTrapAdapter = new ManTrapAdapter(getActivity(), listManTraps);
        rlModule.setAdapter(manTrapAdapter);
        manTrapAdapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                showToast(listManTraps.get(position).getName());
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        bean = (LoginCallbackBean.ResultBean.UserBean)
                SpUtils.getBean(getActivity(),"module_user_bean");
        name = bean.getName();
        //0:正常 1:封控 2:密接 3:次密接 4:阳性
        scanByStatus(bean.getStatus());
        //获取危险地区
        //getManTrapList();
    }


    @Override
    protected int findLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvModuleToScan){
            Intent var7 = new Intent(getActivity(), ScanKitActivity.class);
            var7.putExtra("ScanFormatValue", new HmsScanAnalyzerOptions.Creator().create().mode);
            startActivityForResult(var7,REQUEST_CODE_SCAN_ONE);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || data == null) {
            LogUtils.e("resultCode-->"+ resultCode);
            return;
        }
        HmsScan obj = data.getParcelableExtra(ScanUtil.RESULT);
        if (obj != null) {
            LogUtils.e("二维码内容-->"+ obj.originalValue);
            if (obj.originalValue.contains(",")) {
                toScan(obj.originalValue);
                tvModuleHint.setText(new StringBuffer(name).append("于").append(DateUtil.getNowTime()).
                        append("到访").append(obj.originalValue.split(",")[1]).append("地"));
            }else{
                showToast("非专用二维码");
            }
        }else{
            LogUtils.e("返回二维码内容空");
        }
    }

    /**
     * 获取危险地区
     */
    public void getManTrapList(){
        HTTPAPI.getInstance().getManTrapList(new StringCallback(){
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("获取危险地区成功-->"+ s);
                ManTrapCallbackBean bean = GsonUtils.GsonToBean(s,ManTrapCallbackBean.class);
                if (bean.getCode() == SUCC_CODE) {
                    listManTraps.clear();
                    listManTraps.addAll(bean.getResult());
                    manTrapAdapter.notifyDataSetChanged();
                    if (listManTraps.size() != 0) {
                        tvModuleManTrap.setVisibility(View.VISIBLE);
                    }else{
                        tvModuleManTrap.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtils.e("获取危险地区失败-->"+ e.getMessage());
            }
        });
    }

    /**
     * 扫码提交
     * @param enterpriseId
     */
    public void toScan(String enterpriseId){
        LoginCallbackBean.ResultBean.UserBean bean = (LoginCallbackBean.ResultBean.UserBean) SpUtils.getBean(getActivity(),"module_user_bean");
        HTTPAPI.getInstance().toScan(enterpriseId,bean.getId(),new StringCallback(){
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("扫码提交成功-->"+ s);
                ScanCallbackBean bean = GsonUtils.GsonToBean(s,ScanCallbackBean.class);
                if (bean.getCode() == SUCC_CODE) {
                    scanByStatus(bean.getResult().getType().split(",")[0]);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtils.e("扫码提交失败-->"+ e.getMessage());
            }
        });
    }

    /**
     * 根据status处理二维码显示
     * @param status
     */
    public void scanByStatus(String status){
        if (status.equals("0")){
            tvModuleToScan.setVisibility(View.VISIBLE);
            ivModule.setImageBitmap(BitmapUtil.getStrZxingBitmap(getActivity(),"正常",R.drawable.logo,R.color.color_20be6f));
        }else if(status.equals("2") || status.equals("4")){
            tvModuleToScan.setVisibility(View.GONE);
            ivModule.setImageBitmap(BitmapUtil.getStrZxingBitmap(getActivity(),"密切接触者",R.drawable.logo,R.color.red_f54a51));
        }else if (status.equals("3")){
            tvModuleToScan.setVisibility(View.GONE);
            ivModule.setImageBitmap(BitmapUtil.getStrZxingBitmap(getActivity(),"次密切接触者",R.drawable.logo,R.color.color_eb992f));
        }
    }
}
